<?php


include_once 'Classes/PHPCodeProvider.php';

/*
 * Create the class
 */

$superMemo = new TypeDeclaration('SuperMemo');

/*
 * Add class fields (properties)
 */

$maxAttempts = new Field('MAX_ATTEMPTS');
$maxAttempts->Attributes->set_const(true);
$maxAttempts->InitExpression = new PrimitiveExpression(8);

$attemptNumber = new Field('attemptNumber');
$attemptNumber->Attributes->set_private(true);

$winningCombination = new Field('winningCombination');

$result = new Field('result');
$result->Attributes->set_private(true);

$score = new Field('score');
$score->Attributes->set_private(true);

$WON = new Field('WON');
$WON->Attributes->set_private(true);
$WON->InitExpression = new PrimitiveExpression(false);

$gameFinished = new Field('gameFinished');
$gameFinished->Attributes->set_private(true);
$gameFinished->InitExpression = new PrimitiveExpression(false);

$superMemo->Members->add($maxAttempts);
$superMemo->Members->add($attemptNumber);
$superMemo->Members->add($winningCombination);
$superMemo->Members->add($result);
$superMemo->Members->add($score);
$superMemo->Members->add($WON);
$superMemo->Members->add($gameFinished);

/*
 * Add class methods
 */

$constructor = new Method('__construct');
$constructor->Statements->add(
    new AssignStatement(
        new FieldReferenceExpression('score', new ThisReferenceExpression()),
        new ArrayCreateExpression()));

$constructor->Statements[0]->Right->Initializers->add(
    new BinaryOperatorExpression(BinaryOperator::KEY_VALUE, 
            new PrimitiveExpression('won'), new PrimitiveExpression(0)));

$constructor->Statements[0]->Right->Initializers->add(
    new BinaryOperatorExpression(BinaryOperator::KEY_VALUE,
            new PrimitiveExpression('lost'), new PrimitiveExpression(0)));

$reset = new Method('reset');

$reset->Statements->add(new ExpressionStatement(new MethodInvokeExpression(
    new MethodReferenceExpression('generateWinningCombination', 
            new ThisReferenceExpression()))));

$reset->Statements->add(new AssignStatement(
    new FieldReferenceExpression('attemptNumber', new ThisReferenceExpression()), 
    new PrimitiveExpression(0)));

$reset->Statements->add(new AssignStatement(
    new FieldReferenceExpression('WON', new ThisReferenceExpression()), 
    new PrimitiveExpression(false)));

$resetScore = new Method('resetScore');

/*$resetScore->Statements->add(new AssignStatement(
    new ArrayIndexerExpression, $Right));*/

$superMemo->Members->add($constructor);
$superMemo->Members->add($reset);
$superMemo->Members->add($resetScore);

$options = new CodeGeneratorOptions();
$options->set_BlankLinesBetweenMembers(false);
$writer = new IndentedTextWriter();

$provider = new PHPCodeProvider();
$provider->generateCodeFromType($superMemo, $writer, $options);

echo '<pre>' . str_replace('<br />', '', highlight_string($writer->get_String(), true)) . '<pre/>';

?>
